﻿using AuthCenter.Domain;
using AuthCenter.ErorCode;
using AuthCenter.Infrastructure;
using AuthCenter_API.Write.Applications.Commands.Audit;
using MediatR;

namespace AuthCenter_API.Write.Applications.CommandHandlers.Audit
{
    public class AuditUpdStateCommandHandler : IRequestHandler<AuditUpdStateCommand, APIResult<object>>
    {
        private readonly IBaseRepository<ShenPiModel> _shenpiRepository;

        public AuditUpdStateCommandHandler(IBaseRepository<ShenPiModel> shenpiRepository)
        {
            _shenpiRepository = shenpiRepository;
        }

        public Task<APIResult<object>> Handle(AuditUpdStateCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result =new APIResult<object>();
            var audit = _shenpiRepository.GetAll().Where(x => x.ShenId == request.ShenId && x.IsDel == 0).FirstOrDefault();
            if (audit == null)
            {
                result.Code = APIEnum.UpdFail;
                result.Message = APIEnum.UpdFail.Desc()+"没有找到对应的记录";
                return Task.FromResult(result);
            }
            if(request.State==1)
            {
                audit.State = 1;
                audit.Infor = null;
            }
            else if(request.State==0)
            {
                if(string.IsNullOrEmpty(request.Infor))
                {
                    result.Code = APIEnum.UpdFail;
                    result.Message = APIEnum.UpdFail.Desc() + "请填写原因";
                    return Task.FromResult(result);
                }
                audit.State = 1;
                audit.Infor=request.Infor;
            }
            result.Code = APIEnum.UpdSuccess;
            result.Message = APIEnum.UpdSuccess.Desc();
            _shenpiRepository.Update(audit);
            return Task.FromResult(result);
        }
    }
}
